package com.novellotus.iov.carvice.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.activity.PayRecordDetailActivity;
import com.novellotus.iov.carvice.bean.RxOrder;
import com.novellotus.iov.carvice.interfaces.AdapterBottomInterface;
import com.novellotus.iov.carvice.utils.TimeUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Rich on 2018/3/13.
 */

public class PayRecordListAdapter extends RecyclerView.Adapter implements AdapterBottomInterface {

    final int BOTTOM = 100;
    private boolean showBottom = false;
    private List<RxOrder.RecordsBean> list;
    private Context context;

    public PayRecordListAdapter(List<RxOrder.RecordsBean> list, Context context) {
        this.context = context;
        if (list == null) {
            this.list = new ArrayList<>();
        } else {
            this.list = list;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == list.size()) {
            return BOTTOM;
        } else {
            return 1;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == BOTTOM) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_bottom, parent, false);
            return new BottomHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_pay_record_list, parent, false);
            return new Holder(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position != list.size() && list.get(position) != null) {
            initHolder((Holder) holder, position);
        }
    }


    private void initHolder(Holder holder, final int position) {
        holder.tv_num.setText(list.get(position).getOrderNo());
        holder.tv_statue.setText(list.get(position).getOrderStatusDesc());
        holder.tv_time.setText(TimeUtils.stampTotime(list.get(position).getPayDate() + ""));
        holder.tv_charge_count.setText("充电" + list.get(position).getChargeDegrees() + "度");
        holder.tv_pay.setText("实付款:¥ " + list.get(position).getPayAmount());
        holder.rlt_item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, PayRecordDetailActivity.class);
                intent.putExtra("record",list.get(position));
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        if (showBottom) {
            return list.size() + 1;
        } else {
            return list.size();
        }
    }

    @Override
    public void showBottomLayout() {
        showBottom = list.size() >= 3;
        notifyDataSetChanged();
    }

    @Override
    public void hideBottomLayout() {
        showBottom = false;
        notifyDataSetChanged();
    }

    /**
     * @param list 刷新数据
     */
    public void update(List<RxOrder.RecordsBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    /**
     * @param list 添加下拉时的
     */
    public void addData(List<RxOrder.RecordsBean> list) {
        this.list.addAll(list);
        notifyDataSetChanged();
    }

    class Holder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_num)
        TextView tv_num;
        @BindView(R.id.tv_statue)
        TextView tv_statue;
        @BindView(R.id.img_logo)
        ImageView img_logo;
        @BindView(R.id.tv_station_name)
        TextView tv_station_name;
        @BindView(R.id.tv_station_type)
        TextView tv_station_type;
        @BindView(R.id.tv_time)
        TextView tv_time;
        @BindView(R.id.tv_pay)
        TextView tv_pay;
        @BindView(R.id.tv_charge_count)
        TextView tv_charge_count;
        @BindView(R.id.rlt_item)
        RelativeLayout rlt_item;


        Holder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class BottomHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.rlt_bottom)
        RelativeLayout rlt_bottom;
        @BindView(R.id.tv_bottom)
        TextView tv_bottom;
        @BindView(R.id.view_left)
        View view_left;
        @BindView(R.id.view_right)
        View view_right;

        BottomHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
